package cn.bfay.lion.jingdong.service;

import cn.bfay.lion.jingdong.client.rsp.JdProductBaseInfoRsp;
import cn.bfay.lion.jingdong.client.rsp.JdProductCouponRsp;
import cn.bfay.lion.jingdong.client.rsp.JdProductInfo;
import cn.bfay.lion.jingdong.client.rsp.JdProductPriceRsp;
import cn.bfay.lion.jingdong.client.rsp.JdProductPromotionRsp;
import cn.bfay.lion.jingdong.enums.CouponType;
import cn.bfay.lion.jingdong.enums.PromotionType;
import cn.bfay.lion.jingdong.helper.CalculateType;
import cn.bfay.lion.jingdong.helper.ProductCalculate;
import cn.bfay.lion.jingdong.helper.ProductCalculateHelper;
import cn.bfay.lion.jingdong.manager.JdManager;
import cn.bfay.lion.util.FormatUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * AbstractService.
 *
 * @author wangjiannan
 * @since 2019/11/6
 */
@Slf4j
@Service
public abstract class AbstractService {
    @Resource
    private JdManager jdManager;

    /**
     * 单个处理.
     *
     * @param skuid 商品id
     * @return {@link JdProductInfo}
     */
    protected JdProductInfo crawlJdProductInfo(String skuid) {
        JdProductBaseInfoRsp jdProductBaseInfoRsp = jdManager.getJdProductBaseInfoRsp(skuid);
        JdProductPriceRsp jdProductPriceRsp = jdManager.getJdProductPriceRsp(skuid);
        JdProductPromotionRsp jdProductPromotionRsp = jdManager.getJdProductPromotionRsp(skuid);
        JdProductCouponRsp jdProductCouponRsp = jdManager.getJdProductCouponRsp(skuid);
        return generateJdProductInfo(skuid, jdProductBaseInfoRsp, jdProductPriceRsp, jdProductPromotionRsp, jdProductCouponRsp);
    }

    /**
     * 批量处理.
     *
     * @param skuids      商品ids
     * @param sleepMillis 休眠时间
     * @return {@link JdProductInfo}
     */
    protected List<JdProductInfo> crawlJdProductInfos(List<String> skuids, long sleepMillis) {
        Map<String, JdProductBaseInfoRsp> jdProductBaseInfoRspsMap = jdManager.getJdProductBaseInfoRsps(skuids).stream().collect(Collectors.toMap((JdProductBaseInfoRsp::getSkuid), (value -> value)));
        Map<String, JdProductPriceRsp> jdProductPriceRspsMap = jdManager.getJdProductPriceRsps(skuids).stream().collect(Collectors.toMap((JdProductPriceRsp::getSkuid), (value -> value)));
        Map<String, JdProductPromotionRsp> jdProductPromotionRspsMap = jdManager.getJdProductPromotionRsps(skuids).stream().collect(Collectors.toMap((JdProductPromotionRsp::getSkuid), (value -> value)));
        List<JdProductInfo> jdProductInfos = new ArrayList<>();
        for (String skuid : skuids) {
            JdProductBaseInfoRsp jdProductBaseInfoRsp = jdProductBaseInfoRspsMap.get(skuid);
            JdProductPriceRsp jdProductPriceRsp = jdProductPriceRspsMap.get(skuid);
            JdProductPromotionRsp jdProductPromotionRsp = jdProductPromotionRspsMap.get(skuid);
            JdProductCouponRsp jdProductCouponRsp = jdManager.getJdProductCouponRsp(skuid);
            jdProductInfos.add(generateJdProductInfo(skuid, jdProductBaseInfoRsp, jdProductPriceRsp, jdProductPromotionRsp, jdProductCouponRsp));
            try {
                Thread.sleep(sleepMillis);
            } catch (InterruptedException e) {
                log.error("", e);
            }
        }
        return jdProductInfos;
    }

    private JdProductInfo generateJdProductInfo(String skuid, JdProductBaseInfoRsp jdProductBaseInfoRsp,
                                                JdProductPriceRsp jdProductPriceRsp, JdProductPromotionRsp jdProductPromotionRsp,
                                                JdProductCouponRsp jdProductCouponRsp) {
        JdProductInfo jdProductInfo = new JdProductInfo();
        jdProductInfo.setSkuid(skuid);
        jdProductInfo.setName(jdProductBaseInfoRsp.getName());
        jdProductInfo.setImagePath(jdProductBaseInfoRsp.getImagePath());
        jdProductInfo.setNormalPrice(jdProductPriceRsp.getNormalPrice());
        jdProductInfo.setPlusPrice(jdProductPriceRsp.getPlusPrice());
        if (jdProductPromotionRsp == null || CollectionUtils.isEmpty(jdProductPromotionRsp.getJdProductPromotions())) {
            jdProductInfo.setPromotionType(PromotionType.NONE.getCode());
        } else {
            JdProductPromotionRsp.JdProductPromotion jdProductBestPromotion = jdProductPromotionRsp.getJdProductPromotions().stream()
                .min(Comparator.comparingDouble(JdProductPromotionRsp.JdProductPromotion::getPromotion)).orElse(new JdProductPromotionRsp.JdProductPromotion());
            int type = jdProductBestPromotion.getType();
            jdProductInfo.setPromotionType(type);
            if (PromotionType.EVERY_FULL_MONEY_MINUS_MONEY.getCode() == type) {
                jdProductInfo.setPromotionFull(jdProductBestPromotion.getNeedMoney());
                jdProductInfo.setPromotionMinus(jdProductBestPromotion.getRewardMoney());
                jdProductInfo.setPromotionDes(
                    "每满" + jdProductBestPromotion.getNeedMoney() + "元减" + jdProductBestPromotion.getRewardMoney());
            } else if (PromotionType.FULL_MONEY_MINUS_MONEY.getCode() == type) {
                jdProductInfo.setPromotionFull(jdProductBestPromotion.getNeedMoney());
                jdProductInfo.setPromotionMinus(jdProductBestPromotion.getRewardMoney());
                jdProductInfo.setPromotionDes(
                    "满" + jdProductBestPromotion.getNeedMoney() + "元减" + jdProductBestPromotion.getRewardMoney());
            } else if (PromotionType.FULL_COUNT_DISCOUNT.getCode() == type) {
                jdProductInfo.setPromotionCount(jdProductBestPromotion.getNeedNum());
                jdProductInfo.setPromotionDiscount(FormatUtils.formatDouble(jdProductBestPromotion.getPromotion()));
                jdProductInfo.setPromotionDes(
                    "满" + jdProductBestPromotion.getNeedNum() + "件打" + jdProductBestPromotion.getRebate() + "折");
            } else if (PromotionType.FULL_MONEY_DISCOUNT.getCode() == type) {
                jdProductInfo.setPromotionFull(jdProductBestPromotion.getNeedMoney());
                jdProductInfo.setPromotionDiscount(FormatUtils.formatDouble(jdProductBestPromotion.getPromotion()));
                jdProductInfo.setPromotionDes(
                    "满" + jdProductBestPromotion.getNeedMoney() + "元打" + jdProductBestPromotion.getRebate() + "折");
            } else {
                jdProductInfo.setPromotionType(PromotionType.NONE.getCode());
            }
        }
        if (jdProductPromotionRsp != null && jdProductPromotionRsp.getPrice() != null) {
            Double price = jdProductPromotionRsp.getPrice();
            jdProductInfo.setNormalPrice(FormatUtils.formatDouble(price));
            jdProductInfo.setPlusPrice(FormatUtils.formatDouble(price));
        }
        jdProductInfo.setPlusType(0);
        if (jdProductPromotionRsp != null && jdProductPromotionRsp.getDiscount() != null) {
            Double discount = jdProductPromotionRsp.getDiscount();
            jdProductInfo.setPlusType(1);
            jdProductInfo.setPlusDiscount(FormatUtils.formatDouble(discount));
            jdProductInfo.setPlusDes("95折");
        }


        if (jdProductCouponRsp == null || CollectionUtils.isEmpty(jdProductCouponRsp.getJdProductCoupons())) {
            jdProductInfo.setCouponType(CouponType.NONE.getCode());
        } else {
            JdProductCouponRsp.JdProductCoupon jdProductBestCoupon = jdProductCouponRsp.getJdProductCoupons().stream()
                .min(Comparator.comparingDouble(JdProductCouponRsp.JdProductCoupon::getCoupon)).orElse(new JdProductCouponRsp.JdProductCoupon());
            if (CouponType.FULL_MONEY_MINUS_MONEY.getCode() == jdProductBestCoupon.getType()) {
                jdProductInfo.setCouponType(CouponType.FULL_MONEY_MINUS_MONEY.getCode());
                jdProductInfo.setCouponFull(jdProductBestCoupon.getQuota());
                jdProductInfo.setCouponMinus(jdProductBestCoupon.getDiscount());
                jdProductInfo
                    .setCouponDes("满" + jdProductBestCoupon.getQuota() + "元减" + jdProductBestCoupon.getDiscount());
            } else if (CouponType.FULL_MONEY_DISCOUNT.getCode() == jdProductBestCoupon.getType()) {
                jdProductInfo.setCouponType(CouponType.FULL_MONEY_DISCOUNT.getCode());
                jdProductInfo.setCouponFull(jdProductBestCoupon.getQuota());
                jdProductInfo.setCouponDiscount(jdProductBestCoupon.getCoupon());
                jdProductInfo
                    .setCouponDes("满" + jdProductBestCoupon.getQuota() + "元打" + jdProductBestCoupon.getDiscount() + "折");
            } else {
                jdProductInfo.setCouponType(CouponType.NONE.getCode());
            }
        }

        if (jdProductInfo.getNormalPrice() != null) {
            ProductCalculate normalCalculate =
                ProductCalculateHelper.calculate(CalculateType.NORMAL_PRICE, jdProductInfo);
            ProductCalculate plusCalculate = ProductCalculateHelper.calculate(CalculateType.PLUS_PRICE, jdProductInfo);
            jdProductInfo.setFinalNormalPrice(FormatUtils.formatDouble(normalCalculate.getPrice()));
            jdProductInfo.setFinalNormalPriceCount(normalCalculate.getCount());
            jdProductInfo.setFinalPlusPrice(FormatUtils.formatDouble(plusCalculate.getPrice()));
            jdProductInfo.setFinalPlusPriceCount(plusCalculate.getCount());
        }

        return jdProductInfo;
    }
}
